package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {

    @NonNull
    @Override
    public Adapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        inflater= LayoutInflater.from(context).inflate(R.layout.layout_item,parent,false);
        return new MyViewHolder(inflater);
    }

    List<Map<String,Object>> list;

    View inflater;

    Context context;

    public Adapter(Context context,List<Map<String,Object>> list) {
        this.context=context;
        this.list=list;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.imageView.setImageResource((Integer) Objects.requireNonNull(list.get(position).get("avatar")));
        holder.textView.setText(Objects.requireNonNull(list.get(position).get("username")).toString());

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,MainActivity_detail.class);
                intent.putExtra("avatar", (Integer) Objects.requireNonNull(list.get(position).get("avatar")));
                intent.putExtra("name", Objects.requireNonNull(list.get(position).get("username")).toString());
                intent.putExtra("sex",Objects.requireNonNull(list.get(position).get("sex")).toString());
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        ImageView imageView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imageView_my_item);
            textView=itemView.findViewById(R.id.textView_my_item);
        }
    }
}
